package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.GoodsProductMapper;
import org.linlinjava.litemall.db.models.GoodsProduct;
import org.linlinjava.litemall.db.service.GoodsProductService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsProductServiceImpl extends ServiceImpl<GoodsProductMapper, GoodsProduct>
    implements GoodsProductService {

    @Override
    public List<GoodsProduct> queryByGid(Long gid) {
        QueryWrapper<GoodsProduct> query = new QueryWrapper<>();
        query.eq("goods_id", gid);
        return baseMapper.selectList(query);
    }

    @Override
    public int count() {
        QueryWrapper<GoodsProduct> query = new QueryWrapper<>();
        return baseMapper.selectCount(query);
    }

    @Override
    public void deleteByGid(Long gid) {
        QueryWrapper<GoodsProduct> query = new QueryWrapper<>();
        query.eq("goods_id", gid);
        baseMapper.delete(query);
    }

    @Override
    public int addStock(Long id, Integer num){
        GoodsProduct obj = getById(id);
        obj.setNumber(obj.getNumber() + num);
        return baseMapper.updateById(obj);
    }

    @Override
    public int reduceStock(Long id, Integer num){
        GoodsProduct obj = getById(id);
        obj.setNumber(obj.getNumber() - num);
        return baseMapper.updateById(obj);
    }

}